﻿Public Class frphieuthu

    Dim DS As New List(Of PhieuThu)

    Sub HienThi()
        DS = XL_PhieuThu.LayDS(-1, -1, -1, -1, -1, Me.Tag, Cbx_Thang.SelectedIndex, cbxNam.SelectedItem, Date.MinValue)
        lsvphieuthu.Items.Clear()
        For Each p In DS
            Dim li As ListViewItem = New ListViewItem(p.Ngay)
            li.Tag = p
            li.SubItems.Add(p.SoTien)
            li.SubItems.Add(XL_LoaiThu.Lay(p.LoaiThu).Loai)
            li.SubItems.Add(XL_TaiKhoan.Lay(p.TaiKhoan).TenTK)
            li.SubItems.Add(XL_NguoiDung.Lay(p.NguoiThu).HoTen)
            lsvphieuthu.Items.Add(li)
        Next
    End Sub

    Private Sub btnthempt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthempt.Click
        Dim f As New F20_Them_Phieu_Thu
        f.Tag = Me.Tag
        f.ShowDialog()
        HienThi()
    End Sub

    Private Sub btnsuapt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuapt.Click
        If lsvphieuthu.SelectedItems.Count > 0 Then
            Try
                Dim p As PhieuThu = lsvphieuthu.SelectedItems(0).Tag
                p.SoTien = Integer.Parse(txtsotienpt.Text)
                p.Ngay = dtpngaychungtupt.Value.Date
                p.LoaiThu = cbxloaithunhappt.SelectedValue
                p.NguoiThu = cbxnguoithu.SelectedValue
                p.NguoiLap = cbxnguoitao.SelectedValue
                p.TaiKhoan = cbxtaikhoanpt.SelectedValue
                p.ChiTiet = rtbchitiet.Text
                XL_PhieuThu.CapNhat(p)
                HienThi()
            Catch ex As Exception
                Exit Sub
            End Try
        Else
            MessageBox.Show("Chọn phiếu thu cần cập nhật!", "Thông báo")
        End If
    End Sub

    Private Sub btnxoapt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoapt.Click
        If lsvphieuthu.SelectedItems.Count > 0 Then
            If MessageBox.Show("Bạn muốn xóa vị trí đã chọn?", "Chú ý!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Dim p As PhieuThu = lsvphieuthu.SelectedItems(0).Tag
                Dim kq As Integer = XL_PhieuThu.Xoa(p)
                If kq = 1 Then
                    HienThi()
                    MessageBox.Show("Xóa thành công!")
                Else
                    MessageBox.Show("Xóa không thành công!")
                End If

            End If
        Else
            MessageBox.Show("Chọn phiếu cần xóa!", "Thông báo")
        End If
    End Sub

    Private Sub btnthongke_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthongke.Click
        Dim f As New frTimKiemThuChi
        f.ShowDialog()
    End Sub

    Private Sub btnthoatpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoatpt.Click
        Me.Close()
    End Sub

    Private Sub LinkLabeltl_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabeltl.LinkClicked
        Dim f As New fr_Loaithu()
        f.ShowDialog()
    End Sub

    Private Sub frphieuthu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Cbx_Thang.SelectedIndex = Now.Month
        Dim nam = Date.Now.Year
        Dim i As Integer = 2
        While i >= 0
            cbxNam.Items.Add(nam - i)
            i = i - 1
        End While
        cbxNam.SelectedItem = Date.Now.Year

        cbxloaithunhappt.DataSource = XL_LoaiThu.LayDS()
        cbxloaithunhappt.DisplayMember = "Loai"
        cbxloaithunhappt.ValueMember = "ID"

        cbxnguoitao.DataSource = XL_NguoiDung.LayDS()
        cbxnguoitao.DisplayMember = "Username"
        cbxnguoitao.ValueMember = "ID"

        cbxnguoithu.DataSource = XL_NguoiDung.LayDS()
        cbxnguoithu.DisplayMember = "Username"
        cbxnguoithu.ValueMember = "ID"

        cbxtaikhoanpt.DataSource = XL_TaiKhoan.LayDS(Me.Tag)
        cbxtaikhoanpt.DisplayMember = "TenTK"
        cbxtaikhoanpt.ValueMember = "ID"

        cbxnguoitao.SelectedValue = Me.Tag
        cbxnguoithu.SelectedValue = Me.Tag
        HienThi()
    End Sub

    Private Sub lsvphieuthu_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvphieuthu.SelectedIndexChanged
        If lsvphieuthu.SelectedItems.Count > 0 Then
            Dim p As PhieuThu = lsvphieuthu.SelectedItems(0).Tag
            txtsotienpt.Text = p.SoTien.ToString()
            dtpngaychungtupt.Value = p.Ngay.Date
            cbxloaithunhappt.SelectedValue = p.LoaiThu
            cbxnguoithu.SelectedValue = p.NguoiThu
            cbxnguoitao.SelectedValue = p.NguoiLap
            cbxtaikhoanpt.SelectedValue = p.TaiKhoan
            rtbchitiet.Text = p.ChiTiet
        End If
    End Sub

    Private Sub Cbx_Thang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbx_Thang.SelectedIndexChanged
        HienThi()
    End Sub

    Private Sub cbxNam_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxNam.SelectedIndexChanged
        HienThi()
    End Sub
End Class